/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.ui.text;

import org.eclipse.dltk.internal.ui.editor.ScriptSourceViewer;
import org.eclipse.dltk.ui.text.IColorManager;
import org.eclipse.dltk.ui.text.ScriptPresentationReconciler;
import org.eclipse.dltk.ui.text.ScriptSourceViewerConfiguration;
import org.eclipse.dltk.ui.text.completion.ContentAssistPreference;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.formatter.IContentFormatter;
import org.eclipse.jface.text.information.IInformationPresenter;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.texteditor.ITextEditor;

import com.funkyroach.use.core.completion.USCompletionProcessor;
import com.funkyroach.use.core.completion.USContentAssistPreference;

public class USSourceViewerConfiguration extends ScriptSourceViewerConfiguration {

    private USCodeScanner fCodeScanner;
    private USStringScanner fStringScanner;
    private RuleBasedScanner fCommentScanner;

    public USSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore,
            ITextEditor editor, String partitioning) {
        super(colorManager, preferenceStore, editor, partitioning);
    }

    public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
        return IUSPartitionTokens.US_PARTITION_TYPES;
    }

    @Override
    protected ContentAssistPreference getContentAssistPreference() {
        return USContentAssistPreference.getDefault();
    }

    protected void alterContentAssistant(ContentAssistant assistant) {
        IContentAssistProcessor scriptProcessor = new USCompletionProcessor(getEditor(), assistant,
                IDocument.DEFAULT_CONTENT_TYPE);
        assistant.setContentAssistProcessor(scriptProcessor, IDocument.DEFAULT_CONTENT_TYPE);
        assistant.setContentAssistProcessor(scriptProcessor, IUSPartitionTokens.US_PARTITIONING);

    }

    public IInformationPresenter getOutlinePresenter(ScriptSourceViewer viewer, boolean doCodeResolve) {
        return null;
    }

    protected void initializeScanners() {

        fCodeScanner = new USCodeScanner(getColorManager(), fPreferenceStore);

        fStringScanner = new USStringScanner(getColorManager(), fPreferenceStore);

        fCommentScanner = createCommentScanner(USColorConstants.US_COMMENT, USColorConstants.US_TODO_TAG);

    }

    protected RuleBasedScanner getCommentScanner() {
        return fCommentScanner;
    }

    protected RuleBasedScanner getStringScanner() {
        return fStringScanner;
    }

    @Override
    public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
        return null;
    }
    
    

    public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
        PresentationReconciler reconciler = new ScriptPresentationReconciler();
        reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));

        DefaultDamagerRepairer dr = new DefaultDamagerRepairer(this.fCodeScanner);
        reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
        reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

        dr = new DefaultDamagerRepairer(getStringScanner());
        reconciler.setDamager(dr, IUSPartitionTokens.US_STRING_TYPE);
        reconciler.setRepairer(dr, IUSPartitionTokens.US_STRING_TYPE);

        dr = new DefaultDamagerRepairer(getCommentScanner());
        reconciler.setDamager(dr, IUSPartitionTokens.US_COMMENT_TYPE);
        reconciler.setRepairer(dr, IUSPartitionTokens.US_COMMENT_TYPE);

        return reconciler;

    }
}